package com.contest.util;

import org.apache.commons.lang.time.DateUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

public class DateUtil {

    public static Date yyyyMMddHHmm(String time)
    {
        try
        {
            SimpleDateFormat sdFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            Date date = sdFormatter.parse(time);
            return date;

        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }

    public static String yyyyMMddHHmm(Date date)
    {
        try
        {
            SimpleDateFormat sdFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            String retStrFormatNowDate = sdFormatter.format(date);
            return retStrFormatNowDate;

        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return "";
    }

    public static String HHmm(Date date)
    {
        try
        {
            SimpleDateFormat sdFormatter = new SimpleDateFormat("HH:mm");
            String retStrFormatNowDate = sdFormatter.format(date);
            return retStrFormatNowDate;

        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return "";
    }

    public static String yyyyMMddNow(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(new Date());
    }
    public static String now(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(new Date());
    }
    public static Date toDayLastMillis(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        String temp = currentDate()+" 23:59:59";
        try {
            date =  sdf.parse(temp);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    private static String currentDate(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String date = format.format(new Date());
        return date;
    }

    public static int getTodayTimeDifference(){
        return (int)((toDayLastMillis().getTime()-System.currentTimeMillis())/1000);
    }

    /**
     * Adds a number of minutes to a date returning a new object.
     * The original date object is unchanged.
     *
     * @param date  the date, not null
     * @param amount  the amount to add, may be negative
     * @return the new date object with the amount added
     * @throws IllegalArgumentException if the date is null
     */
    public static Date addMinutes(Date date, int amount) {

        return  DateUtils.addMinutes(date , amount);
    }

    public static String getAfterSecondResult(String quizTime, String afterSecond) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            if(quizTime != null && quizTime.length() > 0 &&
                    afterSecond != null && afterSecond.length() > 0) {
                long time = ((format.parse(quizTime).getTime() / 1000) + Integer.parseInt(afterSecond));
                date = new Date(time * 1000);
            }else{
                return null;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return format.format(date);
    }

    public static String formatTime(Long ms) {
        Integer ss = 1000;
        Integer mi = ss * 60;
        Integer hh = mi * 60;
        Integer dd = hh * 24;

        Long day = ms / dd;
        Long hour = (ms - day * dd) / hh;
        Long minute = (ms - day * dd - hour * hh) / mi;
        Long second = (ms - day * dd - hour * hh - minute * mi) / ss;
        Long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;

        StringBuffer sb = new StringBuffer();
        if(day > 0) {
            sb.append(day+"天");
        }
        if(hour > 0) {
            sb.append(hour+"小时");
        }
        if(minute > 0) {
            sb.append(minute+"分");
        }
        if(second > 0) {
            sb.append(second+"秒");
        }
        if(milliSecond > 0) {
            sb.append(milliSecond+"毫秒");
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(getAfterSecondResult("2018-01-26 10:33:23","300"));
    }
}
